<template>
  <div class="revise-password">
    <cell-group>
      <field :label="'原密码'" type="password" placeholder="请输入原密码" v-model="oldPassword"></field>
      <field :label="'新密码'" type="password" placeholder="请输入新密码" v-model="newPassword"></field>
      <field :label="'确认新密码'" type="password" placeholder="请再次输入新密码" v-model="repassword"></field>
    </cell-group>

    <div class="submit">
      <van-button type="info" size="large" @click="updatePassword" :disabled="isSubmitting" >{{isSubmitting ? '正在提交' : '确认'}}</van-button>
    </div>
  </div>
</template>

<script>
import { Button, Field, Cell, CellGroup, Toast } from "vant";
import http from "../service/myHttp.js";
import urls from "../service/url.js";
import { mapState } from "vuex";
const md5 = require("js-md5");

export default {
  components: {
    vanButton: Button,
    Field,
    Cell,
    CellGroup
  },
  data() {
    return {
      oldPassword: "",
      newPassword: "",
      repassword: "",
      isSubmitting: false
    };
  },
  computed: {
    ...mapState({
      userInfo: state => state.login_module.userinfo,
      countAlias: "userInfo"
    })
  },
  mounted() {},
  methods: {
    // 修改密码
    updatePassword() {
      if(!this.oldPassword || !this.newPassword || !this.repassword){
        Toast('密码均不能为空')
        return false
      }

      if(this.newPassword != this.repassword){
        Toast('两次密码输入不一致')
        return false
      }

      Toast('正在提交，请稍候')
      this.isSubmitting = true
      http
        .post(urls.XGMM, {
          id: this.userInfo.id,
          oldPwd: md5(this.oldPassword),
          newPwd: md5(this.newPassword)
        })
        .then(res => {
          this.isSubmitting = false
          if (res.data.code == 200) {
            Toast.success("修成密码成功");

            setTimeout(() => {
              this.$router.go(-1);
            }, 1000);
          }else{
            Toast(res.data.msg);
          }
        });
    }

    // 验证原密码
    // verifyPassword() {
    //   return new Promise((resolve, reject) => {
    //     http
    //       .post(urls.LOGIN, {
    //         name: this.userInfo.phone,
    //         password: md5(this.oldPassword)
    //       })
    //       .then(res => {
    //         if (res.data.code == 200) {
    //           resolve();
    //         } else {
    //           reject(res.data.msg);
    //         }
    //       });
    //   });
    // },
    // // 修改密码
    // revisePassword() {
    //   this.verifyPassword()
    //     .then(() => {
    //       return http
    //         .post(urls.XGTXMM, {
    //           password: md5(this.newPassword),
    //           id: this.userInfo.id
    //         })
    //         .then(res => {
    //           if (res.data.code == 200) {
    //             Toast.success("修成密码成功");

    //             setTimeout(() => {
    //               this.getHeadImg();
    //             }, 1000);
    //           }
    //         });
    //     })
    //     .catch(err => {
    //       console.log("修改密码过程有误：", err);
    //     });
    // }
  }
};
</script>

<style scoped>
</style>